package cn.sumpu.app.coolsk.biz.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.sumpu.app.coolsk.biz.AddressCodeBiz;
import cn.sumpu.app.coolsk.dao.AddressCodeDao;
import cn.sumpu.app.coolsk.domain.AddressCode;
import cn.sumpu.app.coolsk.exception.AppBusinessException;
import cn.sumpu.toolkit.common.util.Common;


public class AddressCodeBizImpl implements AddressCodeBiz {

	private AddressCodeDao addressCodeDao;

	public void setAddressCodeDao(AddressCodeDao addressCodeDao) {
		this.addressCodeDao = addressCodeDao;
	}

	//取得全部的省/直辖市的名称
	@Override
	public List<AddressCode> getProvinceList() throws AppBusinessException {
		return addressCodeDao.getProvinceList();
	}

	/**
	 * author Bruce Zhang
	 * @param province
	 * @return			根据省/直辖市取得市/区
	 * @throws AppBusinessException
	 */
	@Override
	public List<AddressCode> getCityByProvince(String province)
			throws AppBusinessException {
		return addressCodeDao.getCityByProvince(province);
	}

	/**
	 * author Bruce Zhang
	 * @param city		城市/直辖市的区
	 * @return			根据市/区取得县/街道
	 * @throws AppBusinessException
	 */
	@Override
	public List<AddressCode> getCountyByCity(String city)
			throws AppBusinessException {
		return addressCodeDao.getCountyByCity(city);
	}

	/**
	 * author Bruce Zhang
	 * @param county	县
	 * @return			根据县取得镇/街道
	 * @throws AppBusinessException
	 */
	@Override
	public List<AddressCode> getTownByCounty(String county)
			throws AppBusinessException {
		return addressCodeDao.getTownByCounty(county);
	}

	@Override
	public AddressCode getProvinceByCode(String provinceCode)
			throws AppBusinessException {
		Map<String, Object> map=new HashMap<String, Object>();
		map.put("province", provinceCode);
		map.put("city", "0");
		map.put("county", "0");
		List<AddressCode> list= addressCodeDao.getAddress(map);
		if(list!=null && list.size()>0){
			return list.get(0);
		}
		return null;
	}

	@Override
	public AddressCode getCityByCode(String cityCode)
			throws AppBusinessException {
		Map<String, Object> map=new HashMap<String, Object>();
		map.put("city", cityCode);
		map.put("county", "0");
		List<AddressCode> list= addressCodeDao.getAddress(map);
		if(list!=null && list.size()>0){
			return list.get(0);
		}
		return null;
	}

	@Override
	public AddressCode getCountyByCode(String countyCode)
			throws AppBusinessException {
		Map<String, Object> map=new HashMap<String, Object>();
		map.put("county", countyCode);
		List<AddressCode> list= addressCodeDao.getAddress(map);
		if(list!=null && list.size()>0){
			return list.get(0);
		}
		return null;
	}

	@Override
	public AddressCode getCityCodeByName(String name) throws AppBusinessException {
		AddressCode addr=null;
		if(!Common.empty(name)){
			Map<String, Object> map=new HashMap<String, Object>();
			map.put("value", name);
			//map.put("name", "'%"+name+"%'");
			List<AddressCode> list= addressCodeDao.getAddress(map);
			
			if(list!=null && list.size()>0){
				addr=list.get(0);
			}
		}
		return addr;
	}

}
